﻿using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Linq;

namespace System.Windows.Forms
{
	[ListBindable(false)]
	public class ListViewGroupExCollection : System.Collections.Generic.IList<ListViewGroupEx>, IList
	{
		private System.Collections.Generic.List<ListViewGroupEx> list = new System.Collections.Generic.List<ListViewGroupEx>();
		private ListViewEx listView;

		internal ListViewGroupExCollection(ListViewEx listView)
			: base()
		{
			this.listView = listView;
		}

		public int Count
		{
			get { return list.Count; }
		}

		public bool IsReadOnly
		{
			get { return false; }
		}

		public ListViewGroupEx this[int index]
		{
			get { return list[index]; }
			set { list[index] = value; }
		}

		public ListViewGroupEx this[string key]
		{
			get
			{
				if (this.list != null)
				{
					for (int i = 0; i < this.list.Count; i++)
					{
						if (string.Compare(key, this[i].Name, false, System.Globalization.CultureInfo.CurrentCulture) == 0)
						{
							return this[i];
						}
					}
				}
				return null;
			}
			set
			{
				int num = -1;
				if (this.list != null)
				{
					for (int i = 0; i < this.list.Count; i++)
					{
						if (string.Compare(key, this[i].Name, false, System.Globalization.CultureInfo.CurrentCulture) == 0)
						{
							num = i;
							break;
						}
					}
					if (num != -1)
					{
						this.list[num] = value;
					}
				}
			}
		}

		public int Add(ListViewGroupEx group)
		{
			if (this.Contains(group))
			{
				return -1;
			}
			this.CheckListViewItems(group);
			group.ListView = this.listView;
			int num = this.Add(group);
			if (this.listView.IsHandleCreated)
			{
				this.listView.InsertGroupInListView(this.list.Count, group);
				this.MoveGroupItems(group);
			}
			return num;
		}

		private void MoveGroupItems(ListViewGroupEx group)
		{
			throw new NotImplementedException();
		}

		private void CheckListViewItems(ListViewGroupEx group)
		{
			throw new NotImplementedException();
		}

		public ListViewGroupEx Add(string header)
		{
			var ret = new ListViewGroupEx(header);
			this.Add(ret);
			return ret;
		}

		public void AddRange(IEnumerable<ListViewGroupEx> items)
		{
			this.listView.BeginUpdate();
			foreach (var item in items)
				this.Add(item);
			this.listView.EndUpdate();
		}

		public void AddRange(IEnumerable<string> items)
		{
			this.listView.BeginUpdate();
			foreach (var item in items)
				this.Add(item);
			this.listView.EndUpdate();
		}

		public void Clear()
		{
			if (this.listView.IsHandleCreated)
			{
				for (int j = 0; j < this.Count; j++)
					this.listView.RemoveGroupFromListView(this[j]);
			}
			for (int i = 0; i < this.Count; i++)
				this[i].ListView = null;
			this.list.Clear();
			this.listView.UpdateGroupView();
		}

		public bool Contains(ListViewGroupEx item)
		{
			return list.Contains(item);
		}

		public void CopyTo(ListViewGroupEx[] array, int arrayIndex)
		{
			list.CopyTo(array, arrayIndex);
		}

		public System.Collections.Generic.IEnumerator<ListViewGroupEx> GetEnumerator()
		{
			return list.GetEnumerator();
		}

		public int IndexOf(ListViewGroupEx item)
		{
			return list.IndexOf(item);
		}

		public void Insert(int index, ListViewGroupEx item)
		{
			list.Insert(index, item);
			listView.InsertGroupInListView(index, item);
		}

		public bool Remove(ListViewGroupEx group)
		{
			group.ListView = null;
			if (this.listView.IsHandleCreated)
				this.listView.RemoveGroupFromListView(group);
			return this.list.Remove(group);
		}

		public void RemoveAt(int index)
		{
			Remove(this[index]);
		}

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return this.GetEnumerator();
		}

		int IList.Add(object value)
		{
			if (!(value is ListViewGroupEx))
				throw new ArgumentException();
			return this.Add((ListViewGroupEx)value);
		}

		bool IList.Contains(object value)
		{
			if (!(value is ListViewGroupEx))
				throw new ArgumentException();
			return this.Contains((ListViewGroupEx)value);
		}

		int IList.IndexOf(object value)
		{
			if (!(value is ListViewGroupEx))
				throw new ArgumentException();
			return this.IndexOf((ListViewGroupEx)value);
		}

		void IList.Insert(int index, object value)
		{
			if (!(value is ListViewGroupEx))
				throw new ArgumentException();
			this.Insert(index, (ListViewGroupEx)value);
		}

		bool IList.IsFixedSize
		{
			get { return false; }
		}

		void IList.Remove(object value)
		{
			if (!(value is ListViewGroupEx))
				throw new ArgumentException();
			this.Remove((ListViewGroupEx)value);
		}

		object IList.this[int index]
		{
			get { return this[index]; }
			set
			{
				throw new NotImplementedException();
			}
		}

		void ICollection.CopyTo(Array array, int index)
		{
			if (this.Count > 0)
				Array.Copy(this.ToArray(), 0, array, index, this.Count);
		}

		bool ICollection.IsSynchronized
		{
			get { return true; }
		}

		object ICollection.SyncRoot
		{
			get { return this; }
		}

		void ICollection<ListViewGroupEx>.Add(ListViewGroupEx item)
		{
			this.Add(item);
		}
	}

	internal class ListViewGroupExCollectionEditor : CollectionEditor
	{
		// Fields
		private object editValue;

		// Methods
		public ListViewGroupExCollectionEditor(Type type) : base(type)
		{
		}

		public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
		{
			this.editValue = value;
			object obj2 = base.EditValue(context, provider, value);
			this.editValue = null;
			return obj2;
		}

		protected override object CreateInstance(Type itemType)
		{
			ListViewGroupEx group = (ListViewGroupEx)base.CreateInstance(itemType);
			group.Name = this.CreateListViewGroupName((ListViewGroupExCollection)this.editValue);
			return group;
		}

		private string CreateListViewGroupName(ListViewGroupExCollection lvgCollection)
		{
			string str = "ListViewGroupEx";
			INameCreationService service = base.GetService(typeof(INameCreationService)) as INameCreationService;
			IContainer container = base.GetService(typeof(IContainer)) as IContainer;
			if ((service != null) && (container != null))
			{
				str = service.CreateName(container, typeof(ListViewGroupEx));
			}
			while (char.IsDigit(str[str.Length - 1]))
			{
				str = str.Substring(0, str.Length - 1);
			}
			int num = 1;
			string str2 = str + num.ToString(System.Globalization.CultureInfo.CurrentCulture);
			while (lvgCollection[str2] != null)
			{
				num++;
				str2 = str + num.ToString(System.Globalization.CultureInfo.CurrentCulture);
			}
			return str2;
		}
	}
}